package cn.chinaunicom.sdsi.frm.workflow.sample.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.workflow.bean.WorkflowEntityForStart;
import cn.chinaunicom.sdsi.frm.workflow.common.WorkflowConstants;
import cn.chinaunicom.sdsi.frm.workflow.component.WorkflowManager;
import cn.chinaunicom.sdsi.frm.workflow.sample.service.ActTestService;

@Service("actTestService")
public class ActTestServiceImpl implements ActTestService {

	/**
	 * 工作流封装组件
	 */
	protected WorkflowManager workflowManager;

	@Override
	public void startProcess(String processDefinitionKey, String assignee, String taskDefinitionKey) {
		/*
		 * 首先处理业务逻辑，放在同一事务中
		 */
		String title = "电子公函管理申请一";
		String userId = "10001";
		Date startTime = new Date();
		// 对应业务主数据主键
		String businessKey = "1001";

		WorkflowEntityForStart wefs = new WorkflowEntityForStart(processDefinitionKey, title, userId, startTime,
				businessKey);

		// 发起时必须存入assignee任务受理人的键值
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("assignee", assignee);
		// 流程全局变量存放处——键值对，没有需要切勿存入参数
		// vars.put("param", "testParam");

		String processInstanceId = "";
		if (taskDefinitionKey == null) {
			processInstanceId = workflowManager.startProcess(wefs, vars, "发起", true);
		} else {
			processInstanceId = workflowManager.startProcess(wefs, vars, "发起", true, taskDefinitionKey);
		}
		System.out.println("title=" + title);
		System.out.println("流程发起后实例ID为：" + processInstanceId);
	}

	/**
	 * 普通环节流程推进方法
	 * 
	 * @param processInstanceId
	 *            流程实例ID
	 * @param taskId
	 *            任务ID
	 * @param taskDefinitionKey
	 *            任务定义KEY
	 * @param assignee
	 *            下环节任务处理人ID
	 * @param handleType
	 *            处理方式：1通过，2回退
	 * @param withdraw
	 *            当前推进操作是否可回退
	 * @param comment
	 *            用户处理意见
	 */
	@Override
	public void pushProcess(String processInstanceId, String taskId, String taskDefinitionKey, String assignee,
			int handleType, int withdraw, String comment) {

		/*
		 * 参数处理，handleType值为1时为通过，2时为回退 withdraw的值为1时为过程不可逆，0时为可撤回
		 */
		String handleTypeStr = "";
		if (handleType == 1) {
			handleTypeStr = WorkflowConstants.HANDLE_TYPE_PASS;
		} else if (handleType == 2) {
			handleTypeStr = WorkflowConstants.HANDLE_TYPE_BACK;
		}
		boolean isWithdraw = withdraw == 1 ? true : false;

		/*
		 * 首先处理业务逻辑，放在同一事务中
		 */
		Map<String, Object> vars = new HashMap<String, Object>();

		if (!"".equals(assignee)) {
			vars.put("assignee", assignee);
		}

		vars.put("handleType", handleTypeStr);
		if (taskDefinitionKey == null || "".equals(taskDefinitionKey)) {
			// 此方法已过期，后期将删除，请禁用
			workflowManager.pushProcess(taskId, processInstanceId, vars, comment, isWithdraw);
		} else {
			workflowManager.pushProcess(taskId, processInstanceId, vars, comment, isWithdraw, taskDefinitionKey);
		}
	}

	@Override
	public void breakProcess(String processInstanceId, String taskId, String comment) {

		workflowManager.deleteProcessIntanceButHistoric(processInstanceId, taskId, comment);
	}

	@Override
	public void withdrawProcess(String processInstanceId, String taskId) {
		workflowManager.withdrawProcess(taskId, processInstanceId);
	}

	@Autowired
	public void setWorkflowManager(WorkflowManager workflowManager) {
		this.workflowManager = workflowManager;
	}

}
